package com.meida.student.app.user.handler;

import com.meida.common.base.entity.EntityMap;
import com.meida.common.base.utils.FlymeUtils;
import com.meida.common.mybatis.interceptor.SaveInterceptor;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.mybatis.query.CriteriaSave;
import com.meida.common.security.OpenHelper;
import com.meida.common.utils.ApiAssert;
import com.meida.common.utils.DateUtils;
import com.meida.common.utils.RedisUtils;
import com.meida.module.bus.client.entity.BusOrder;
import com.meida.module.bus.client.entity.BusOrderAnswer;
import com.meida.module.bus.client.entity.BusOrderQuestion;
import com.meida.module.bus.client.enums.OrderStatePEnum;
import com.meida.module.bus.provider.service.BusOrderAnswerService;
import com.meida.module.bus.provider.service.BusOrderQuestionService;
import com.meida.module.bus.provider.service.BusOrderService;
import com.meida.module.user.client.entity.AppUser;
import com.meida.module.user.provider.service.AppUserService;
import com.meida.msg.client.entity.MsgReceive;
import com.meida.msg.provider.service.MsgReceiveService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 订单追问保存
 *
 * @author zyf
 */
@Component
public class OrderQuestionSaveHandler implements SaveInterceptor<BusOrderQuestion> {

    @Autowired
    private BusOrderService busOrderService;

    @Autowired
    private AppUserService appUserService;

    @Autowired
    private BusOrderAnswerService busOrderAnswerService;

    @Autowired
    private MsgReceiveService msgReceiveService;

    @Autowired
    private RedisUtils redisUtils;

    @Override
    public ResultBody validate(CriteriaSave cs, EntityMap params) {
        Long orderId = cs.getLong("orderId");
        BusOrder busOrder = busOrderService.getById(orderId);
        String orderState = busOrder.getOrderState();
        //订单如果完成则不可追问
        if ("5".equals(orderState)) {
            return ResultBody.failed("订单已完成,不可追问");
        }
        ApiAssert.isNotEmpty("缺少orderId参数", orderId);
        ApiAssert.isNotEmpty("该订单不存在", busOrder);
        BusOrderQuestion busOrderQuestion = cs.getEntity(BusOrderQuestion.class);
        String askContent = busOrderQuestion.getContent();
        ApiAssert.isNotEmpty("请填写内容", askContent);
        EntityMap settleConfig = redisUtils.getConfigMap("SETTLE_CONFIG");
        Integer orderCanQuestionDay = settleConfig.getInt("orderCanQuestionDay");
        BusOrderAnswer busOrderAnswer = busOrderAnswerService.getAnswerByOrderId(orderId);
        Date checkTime = busOrderAnswer.getCheckTime();
        Date canQuestionDate = DateUtils.plusDateDays(checkTime, orderCanQuestionDay);
        String canQuestionDateStr = DateUtils.formatDate(canQuestionDate, "yyyy-MM-dd HH:mm:ss");
        Boolean isGt = DateUtils.gt(DateUtils.getDateTime(), canQuestionDateStr);
        if (isGt) {
            ApiAssert.failure("超过可追问日期");
        }
        return ResultBody.ok();
    }

    @Override
    public void prepare(CriteriaSave criteriaSave, EntityMap entityMap, BusOrderQuestion busOrderQuestion) {
        Long userId = OpenHelper.getUserId();
        busOrderQuestion.setUserId(userId);
        AppUser appUser = appUserService.getById(userId);
        if (FlymeUtils.isNotEmpty(appUser)) {
            busOrderQuestion.setUserType(appUser.getUserType());
        }
    }

    @Override
    public void complete(CriteriaSave cs, EntityMap params, BusOrderQuestion busOrderQuestion) {
        Long orderId = busOrderQuestion.getOrderId();
        Long userId = busOrderQuestion.getUserId();
        BusOrder busOrder = busOrderService.getById(orderId);
        Long answerUserId = busOrder.getAnswerUserId();
        //说明是学霸
        String msgTitle = "订单追问通知";
        if (answerUserId.longValue() == userId.longValue()) {
            String msgContent = "恭喜您！您的追问信息学霸已回复请及时查看！感谢您使用留同学APP";
            MsgReceive msgReceive = msgReceiveService.sendMsgReceiveNotice(BusOrder.class.getSimpleName(), userId, msgTitle, msgContent);
            busOrderService.orderPushMessage(msgReceive, msgTitle, msgContent);
        } else {
            AppUser appUser = appUserService.getById(userId);
            String msgContent = appUser.getNickName() + "已提交追问信息，请您及时回复解答！感谢您使用留同学APP";
            MsgReceive msgReceive = msgReceiveService.sendMsgReceiveNotice(BusOrder.class.getSimpleName(), answerUserId, msgTitle, msgContent);
            busOrderService.orderPushMessage(msgReceive, msgTitle, msgContent);
        }

    }

}
